package com.mer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mer.entity.TeacherCollect;
import com.mer.mapper.TeacherCollectMapper;
import com.mer.service.TeacherCollectService;
import com.mer.vo.AttentionList;
import com.mer.vo.AttentionVo;
import com.mer.vo.CommonResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: IIPhase-project
 * @description: 教师收藏实现类
 * @author: 张庆恒
 * @create: 2021-10-21 16:53
 **/
@Service
public class TeacherCollectServiceImpl extends ServiceImpl<TeacherCollectMapper, TeacherCollect> implements TeacherCollectService {

    @Resource
    private TeacherCollectMapper teacherCollectMapper;
    /**
     * 添加关注
     *
     * @param attentionVo
     * @return
     */
    @Override
    public CommonResult addAttentionTeacher(AttentionVo attentionVo) {
        QueryWrapper<TeacherCollect> wrapper = new QueryWrapper<>();
        wrapper.eq("member_id", attentionVo.getMemberid());
        wrapper.eq("teacher_id", attentionVo.getTeacherid());
        TeacherCollect teacherCollect = teacherCollectMapper.selectOne(wrapper);
        if (teacherCollect == null) {
            TeacherCollect teacher = new TeacherCollect();
            teacher.setTeacherId(attentionVo.getTeacherid());
            teacher.setMemberId(attentionVo.getMemberid());
            System.out.println(teacher);

            int i = teacherCollectMapper.insert(teacher);
            if (i > 0) {
                return new CommonResult(2000, "收藏成功", null);
            }
        }
        return new CommonResult(5000, "已添加收藏", null);
    }

    /**
     * 取消关注
     *
     * @param attentionVo
     * @return
     */
    @Override
    public CommonResult cancelAttentionTeacher(AttentionVo attentionVo) {
        QueryWrapper<TeacherCollect> wrapper = new QueryWrapper<>();
        wrapper.eq("member_id",attentionVo.getMemberid());
        wrapper.eq("teacher_id",attentionVo.getTeacherid());
        int i = teacherCollectMapper.delete(wrapper);
        if(i>0){
            return new CommonResult(2000,"取关成功",null);
        }
        return new CommonResult(5000,"取关失败",null);
    }

    /**
     * 加载页面时  显示是否关注该讲师
     *
     * @param attentionVo
     * @return
     */
    @Override
    public CommonResult findAttentionTeacher(AttentionVo attentionVo) {
        QueryWrapper<TeacherCollect> wrapper = new QueryWrapper<>();
        wrapper.eq("member_id",attentionVo.getMemberid());
        wrapper.eq("teacher_id",attentionVo.getTeacherid());
        TeacherCollect teacherCollect = teacherCollectMapper.selectOne(wrapper);
        if (teacherCollect!=null){
            attentionVo.setStatus(1);
            return new CommonResult(2000,"查询成功",attentionVo);
        }else {
            attentionVo.setStatus(0);
            return new CommonResult(2000,"查询成功",attentionVo);
        }
    }

    /**
     * 查看当前所关注的讲师
     * @param memberid
     * @return
     */
    @Override
    public CommonResult findAttentionTeacherById(String memberid) {
        List<AttentionList> courseById = teacherCollectMapper.findAttentionTeacherById(memberid);
        if (courseById.size()>0){
            return new CommonResult(2000,"查询成功",courseById);
        }
        return new CommonResult(5000,"暂无收藏",courseById);
    }
}
